<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		errorModal,
		ignoreHtml,
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		changeConfigAttr,
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		hasEditor,
		getEditorTitle,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	import moment from "@/static/js/moment.js"
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				editId:""
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		methods:{
			verifySubDel(index,editor){
				let _that = this
				if(editor.workList[index].upperFlag){
					if(editor.groupKey == "title_index"){
						_that.om.toast('不能删除上级分解的指标')
					}else if(editor.groupKey == "title_work"){
						_that.om.toast('不能删除上级分解的工作项')
					}else{
						_that.om.toast('不能删除上级分解的数据')
					}
					
					return false
				}else{
					return true
				}
			},
			getWorkParam4(){
				let _that = this
				return {
					instId:_that.$refs.autoform.formdata.instId,
					indexList:JSON.stringify(_that.subEditorObj["title_index"].workList)
				}
			},
			formSubmit(){
				let _that = this
				let workArr = JSON.parse(_that.formdata.workStr)
				for(let i=0; i<workArr.length; i++){
					workArr[i].monthWorkList = null
				}
				_that.formdata.workStr = JSON.stringify(workArr)
				_that.om.formSave("tuserAnnualTargetPlan/addTuserAnnualTargetPlan",_that.formdata,_that)
			},
			getWorkParam1(){
				let _that = this
				return {
					title:getEditorTitle("title_target","orderNum",_that.subConfigData["title_target"]),
					targetList:JSON.stringify(_that.subEditorObj["title_target"].workList)
				}
			},
			getWorkParam2(){
				let _that = this
				return {
					title:(_that.subConfigData["title_index"]?getEditorTitle("title_index","indexId",_that.subConfigData["title_index"]):''),
					indexList:_that.subConfigData["title_index"]?JSON.stringify(_that.subEditorObj["title_index"].workList):'',
					busCode:_that.busCode
				}
			},
			getOthers(item){
				let _that = this
				_that.om.request(
					"tuserAnnualTargetPlan/getOthers",
					{
						year:item.value
					}
				).then(res=>{
					if(_that.$refs.autoform.hasEditor("title_index","indexStr")){
						_that.subEditorObj["title_index"].workList = res.value.indexList?res.value.indexList:[]
					}
					if(_that.$refs.autoform.hasEditor("title_target","targetStr")){
						_that.subEditorObj["title_target"].workList = res.value.targetList?res.value.targetList:[]
					}
					if(_that.$refs.autoform.hasEditor("title_work","workStr")){
						_that.subEditorObj["title_work"].workList = res.value.workList?res.value.workList:[]
					}
					if(_that.$refs.autoform.hasEditor("title_mission","mission")){
						_that.$refs.autoform.formdata.mission = res.value.mission
					}
					if(_that.$refs.autoform.hasEditor("title_iatpTarget","target")){
						_that.$refs.autoform.formdata.target = res.value.target
					}
				})
			},
			findAllIndexEchart(item) {
				let _that = this
				_that.om.request(
					"tmonthlyPlan/findAllIndexEchart", {
						userId: _that.userId,
						year: (Number(item.value)-1)
					}
				).then(res => {
					let arr = []
					for (let index in res.value) {
						let obj = {
							categories: [],
							series: [],
							title: "",
							type: 'line'
						}
						obj.categories = res.value[index].xAxis.data
						obj.series = res.value[index].series
						obj.title = res.value[index].title
						obj.name = res.value[index].yAxis.name
						obj.listTitle = ["折线图", "柱状图"]
						obj.listValue = ["line", "column"]
						arr.push(obj)
					}
					if(_that.subEditorObj["title_chart"]){
						_that.subEditorObj["title_chart"].workList = arr
					}
				})
			},
			onSelectYear(item) {
				let _that = this
				_that.om.request(
					"tuserAnnualTargetPlan/verifyYear", {
						year: item.value,
						editId: _that.editId
					}
				).then(res => {
					if (res.succeed && !res.value) {
						_that.findAllIndexEchart(item)
						if(!_that.edit){
							_that.getOthers(item)
						}
						let lastDay = moment(item.value + '').endOf("year").format("YYYY-MM-DD")
						let fitstDy = moment(item.value + '').startOf("year").format("YYYY-MM-DD")
						_that.$refs.autoform.formdata.startDate = fitstDy
						_that.$refs.autoform.changeEditor("title_basic", "startDate", "type", "datebox")
						_that.$refs.autoform.changeEditor("title_basic", "startDate", "max", lastDay)
						_that.$refs.autoform.changeEditor("title_basic", "startDate", "min", fitstDy)
						_that.$refs.autoform.formdata.endDate = lastDay
						_that.$refs.autoform.changeEditor("title_remark","remark","type","textarea")
						_that.$refs.autoform.changeEditor("title_remark","remark","prompt","请输入")
					}else{
						errorModal(_that,"提示信息",ignoreHtml(res.value),()=>{
							_that.$refs.autoform.clear("title_basic","year")
						})
					}
				})
			},
			initLoad(options){
				let _that = this
				let ajaxArr = [
					urlUtil.zposUrl(options.code),
					{
						url:"commonForm/getInitParamMap",
						param:{
							formCode:options.code
						},
					}
				]
				let fileId = options.fileId
				if(fileId){
					_that.editId = fileId
					ajaxArr.push({
						url:"tuserAnnualTargetPlan/getInfo",
						param:{
							id:fileId
						},
						method:"post"
					})
				}
				formModelLoad(ajaxArr,function(res,mainData){
					uni.setStorageSync('objFormCode', JSON.stringify(res[1].value))
					changeEditor("topinfo","zposId","data",configSelectData(res[0],"id","posname"),mainData)
					var x = new Date().getFullYear();
					let arr = []
					for (let i = -3; i <2 ; i++) {
						let obj = {
							name: x+i,
							value: x+i
						}
						arr.push(obj)
					}
					changeEditor("title_basic", "year", "data", configSelectData(arr, "name", "value"),mainData)
					if(_that.subEditorObj["title_target"]){
						let att = {}
						let configData = _that.subConfigData["title_target"]
						if(hasEditor("title_target","orderNum",configData)){
							att.titleTemplate = '<%=orderNum || "未填写"%>'
						}
						changeEditor("title_target","targetStr",att,mainData)
					}
					if(_that.subEditorObj["title_work"]){
						let att = {
							getWorkParam:"getWorkParam2"
						}
						let configData = _that.subConfigData["title_work"]
						if(hasEditor("title_work","workName",configData)){
							att.titleTemplate ='<%=workName || "未填写"%>'
						} 
						changeEditor("title_work","workStr",att,mainData)
					}
					if(_that.subEditorObj["title_index"]){
						let att = {
							getWorkParam:"getWorkParam1"
						}
						let configData = _that.subConfigData["title_index"]
						if(hasEditor("title_index","indexId",configData)){
							att.titleTemplate = '<%=indexName || "未填写"%>'
						}
						changeEditor("title_index","indexStr",att,mainData)
					}
					if(_that.subEditorObj["title_monthIndex"]){
						let att = {
							getWorkParam:"getWorkParam4"
						}
						let configData = _that.subConfigData["title_monthIndex"]
						if(hasEditor("title_monthIndex","orderNum",configData)){
							att.titleTemplate = "<%= orderNum %>"
						}
						if(hasEditor("title_monthIndex","indexName",configData)){
							let title = getEditorTitle("title_monthIndex","indexName",configData)
							att.subtitle1Template = title+":<%= indexName %>"
						}
						changeEditor("title_monthIndex","monthIndexStr",att,mainData)
					}
					if(fileId){
						let fieldArr = getFormFields(mainData)
						fieldArr.push("id")
						if(fieldArr.includes("target")){
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"target",res[2].value.target)
							_that.$set(_that.formdata,"target",res[2].value.target)
						}
						if(fieldArr.includes("mission")){
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"mission",res[2].value.mission)
							_that.$set(_that.formdata,"mission",res[2].value.mission)
						}
						for(let index in res[2].value.tuserAnnualTargetPlan){
							if(fieldArr.includes(index)){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[2].value.tuserAnnualTargetPlan[index])
								_that.$set(_that.formdata,index,res[2].value.tuserAnnualTargetPlan[index])
							}
						}
						
						let targetList = _that._.cloneDeep(res[2].value.targetList)
						_that.subEditorObj["title_target"].workList = targetList
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"targetStr",JSON.stringify(targetList))
						_that.$set(_that.formdata,"targetStr",JSON.stringify(targetList))
						
						let indexList = _that._.cloneDeep(res[2].value.indexList)
						_that.subEditorObj["title_index"].workList = indexList
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"indexStr",JSON.stringify(indexList))
						_that.$set(_that.formdata,"indexStr",JSON.stringify(indexList))
						
						let workList = _that._.cloneDeep(res[2].value.workList)
						_that.subEditorObj["title_work"].workList = workList
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"workStr",JSON.stringify(workList))
						_that.$set(_that.formdata,"workStr",JSON.stringify(workList))
						
						if(hasEditor('title_monthIndex','monthIndexStr',mainData)){
							let monthIndexList = _that._.cloneDeep(res[2].value.monthIndexList)
							_that.subEditorObj["title_monthIndex"].workList = monthIndexList
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"monthIndexStr",JSON.stringify(monthIndexList))
							_that.$set(_that.formdata,"monthIndexStr",JSON.stringify(monthIndexList))
						}
						_that.$nextTick(function(){
							_that.$refs.autoform.initSelectIndex("topinfo","zposId")
							_that.$refs.autoform.initSelectIndex("title_basic","year")
							if(_that.$refs.autoform.formdata.year)
							_that.onSelectYear({value:_that.$refs.autoform.formdata.year})
						})
					}
				},options)
			}
		},
		onLoad(options) {  
			let _that = this
			_that.initLoad(options)
		},
		onUnload() {
			//#ifdef APP-PLUS
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					uni.removeStorage({
						key: getWorkCacheKey(fieldId)
					});
				}
			}
			//#endif
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				for(let fieldId in _that.subEditorObj){
					let editor = _that.subEditorObj[fieldId]
					uni.getStorage({
						key:getWorkCacheKey(fieldId),
						success:(res)=>{
							editor.workList = res.data
							uni.removeStorage({
								key:getWorkCacheKey(fieldId)
							})
						}
					})
				}
			}
		}
	}
</script>
<style></style>                                                                                                                                                                                                                                                                                                                  
